package com.atguigu.gulimall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ ClassName ThreadTest2
 * @ Description TODO
 * @ Author admin
 * @ Date 2021/3/15 16:10
 * @ Version 1.0
 */
public class ThreadTest2 {
    //线程池
    public static ExecutorService execute = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /**
         * 线程串行化
         *
         * * thenRun：不能获取上一步的执行结果
         * * thenAcceptAsync：能接受上一步结果，但是无返回值
         * * thenApplyAsync：能接受上一步结果，有返回值
         */
//        CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程id: " + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果: " + i);
//            return i;
//        }, execute).thenRunAsync(()->{
//            System.out.println("任务二启动了..."+"当前线程id: "+Thread.currentThread().getId());
//        },execute);
//        CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程id: " + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果: " + i);
//            return i;
//        }, execute).thenAcceptAsync((result)->{
//            System.out.println("任务二启动了..."+"结果"+result+"当前线程id: "+Thread.currentThread().getId());
//        }, execute);
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程id: " + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果: " + i);
            return i;
        }, execute).thenApplyAsync(res -> {
            return res;
        });
        Integer integer = integerCompletableFuture.get();
        


    }
}
